<?php

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\models\Attribute;
use App\models\Attribute_value;
use App\models\Category;
class GoodsAttributeController extends Controller
{
    //展示属性
    public function show_attribute(){
        $attribute = new Attribute();
        $data = $attribute->join('category','category.id','=','attribute.goods_type_id')->get()->toArray();
    	return view("goodsAttribute/attribute_list",['data'=>$data]);
    }
    //添加属性
    public function add_attribute(){
        
        $cat_data = Category::where('parent_id',0)->get()->toArray();
    	return view("goodsAttribute/attribute_add",['cat_data'=>$cat_data]);
    }
    public function doadd_attribute(Request $request){
        $data =$request->input();
        foreach ($data['is_show'] as $key => $value) {
        	$data['is_show'] = $value;	
        }
        $attribute = new Attribute();
        unset($data['_token']);
        $res = $attribute->insert($data);
        if($res){
        	echo json_encode(['code'=>'0','msg'=>'添加成功']);
        }else{
        	echo json_encode(['code'=>'1','msg'=>'添加失败']);
        }
    }
    //修改属性
    public function edit_attribute(Request $request){
      if($request->isMethod('get')){
      	 $attr_id=$request->input('attr_id');
      	 $data = Attribute::where('attr_id',$attr_id)->first();
      	 $cat_data = Category::where('parent_id',0)->get()->toArray();
      	 return view('goodsAttribute/attribute_edit',['data'=>$data,'cat_data'=>$cat_data]);
      }
      if($request->isMethod('post')){
      	$data =$request->input();
        $attribute = new Attribute();
        unset($data['_token']);
        $id = $data['attr_id'];
        unset($data['attr_id']);
        $res = $attribute->where('attr_id',$id)->update($data);
        if($res){
        	echo json_encode(['code'=>'0','msg'=>'修改成功']);
        }else{
        	echo json_encode(['code'=>'1','msg'=>'修改失败']);
        }
      }
    }
    //删除属性
    public function del_attribute(Request $request){
    	$id=$request->input()['id'];
    	$attribute = new Attribute();
    	$res =$attribute->where('attr_id','=',$id)->delete();
        if($res){
        	echo json_encode(['code'=>'0','msg'=>'删除成功']);
        }else{
        	echo json_encode(['code'=>'1','msg'=>'删除失败']);
        }
    }
    //属性的状态更改
    public function static_attribute(Request $request){
    	$data = $request->input();
    	$id = $data['id'];
    	unset($data['id']);
    	if(Attribute::where('attr_id',$id)->update($data)){
    		echo json_encode(['code'=>'0','msg'=>'更改成功']);
    	}else{
    		echo json_encode(['code'=>'1','msg'=>'更改失败']);
    	}
    }
    //属性值查看
    public function attr_value_check(Request $request){
    	$attr_id=$request->input('attr_id');
        $attribute_value = new Attribute_value();
        $value_data = $attribute_value->where('attr_value_id',$attr_id)->get()->toArray();
        return view('goodsAttribute/attr_value_list',['value_data'=>$value_data,'attr_id'=>$attr_id]);
    }
    //属性值添加
    public function attr_value_add(Request $request){
    	if($request->isMethod('get')){
    		$attr_id=$request->input('attr_id');
    	    return view('goodsAttribute/attr_value_add',['attr_id'=> $attr_id]);
    	}
    	if($request->isMethod('post')){
    		$data = $request->input();
           unset($data['_token']);
            $res = Attribute_value::insert($data);
        if($res){
        	echo json_encode(['code'=>'0','msg'=>'添加成功']);
        }else{
        	echo json_encode(['code'=>'1','msg'=>'添加失败']);
        }
    	}
    }
    //属性值编辑
    public function attr_value_edit(Request $request){
    	if($request->isMethod('get')){
      	 $id=$request->input('id');
      	 $data = Attribute_value::where('id',$id)->first()->toArray();
      	 return view('goodsAttribute/attr_value_edit',['data'=>$data]);
      }
      if($request->isMethod('post')){
      	$data =$request->input();
        unset($data['_token']);
        $id = $data['id'];
        unset($data['id']);
        $res = Attribute_value::where('id',$id)->update($data);
        if($res){
        	echo json_encode(['code'=>'0','msg'=>'修改成功']);
        }else{
        	echo json_encode(['code'=>'1','msg'=>'修改失败']);
        }
      }
    }
    //属性值删除
     public function attr_value_del(Request $request){
    	$id=$request->input()['id'];
    	$res =Attribute_value::where('id','=',$id)->delete();
        if($res){
        	echo json_encode(['code'=>'0','msg'=>'删除成功']);
        }else{
        	echo json_encode(['code'=>'1','msg'=>'删除失败']);
        }
    }

    

}